function accountStrTest(acc) {
    return new RegExp("^.{8,16}$").test(acc);
}

function idStrTest(id) {
    return new RegExp("[^<>/\"'$]$").test(id);
}

function logout(goback) {
	cleanUserInfo();
	localStorage.setItem("OESLoaded", "false");

	if(goback) location.href = "login.jsp";
}

function getUserInfo(data, uname) {
	//All is String

	var uid     = data.userid;
	var unick   = data.usernick;
	var ubank   = data.userbank;
	var uscore  = data.userscore;
	var utel    = data.usertel;
	var uvip    = data.uservip;
	var upic    = data.userpic;
	var uqq     = data.userqq;
	var uwechat = data.userwechat;

	localStorage["OESuserid"]     = uid;
	localStorage["OESusernick"]   = unick;
	localStorage["OESuserbank"]   = ubank;
	localStorage["OESuserscore"]  = uscore;
	localStorage["OESusertel"]    = utel;
	localStorage["OESuservip"]    = uvip;
	localStorage["OESuserpic"]    = upic;
	localStorage["OESuserqq"]     = uqq;
	localStorage["OESuserwechat"] = uwechat;

	localStorage["OESLoaded"] = "true";
}

function cleanUserInfo() {

	localStorage.removeItem("OESuserid");
	localStorage.removeItem("OESusernick");
	localStorage.removeItem("OESuserbank");
	localStorage.removeItem("OESuserscore");
	localStorage.removeItem("OESusertel");
	localStorage.removeItem("OESuservip");
	localStorage.removeItem("OESuserpic");
	localStorage.removeItem("OESuserqq");
	localStorage.removeItem("OESuserwechat");

	sessionStorage.removeItem("OESloginStat");
	localStorage.removeItem("OESloginStat");
	localStorage.removeItem("OESloginPass");
}

function userLogin(uname, upass, autoLogin, successCall, failCall) {
	$.ajax({
		url:"login.userInfoService",
		type:"post",
		data:{
			username:uname,
			password:upass
		},
		dataType:"json",
		success:function(data) {
			var loginState = data.OESloginStat;
			localStorage.setItem("OESloginStat", loginState);
			switch (loginState) {
				case "succeed":
					if(autoLogin) localStorage.setItem("OESloginStat", "auto");
					localStorage.setItem("OESloginName", uname);
					localStorage.setItem("OESloginPass", upass);
					sessionStorage.setItem("OESloginStat", "succeed");

					getUserInfo(data, uname);
					if(successCall != null) successCall();
					return;
				default:if(failCall != null) failCall(loginState);
			}
		}
	});
}

function userRegister(uname, upass, unick, successCall, failCall) {
	
	upass = sha256.hex(upass);
	$.ajax({
		url:"register.userInfoService",
		type:"post",
		data:{
			username:uname,
			password:upass,
			usernick:unick
		},
		dataType:"json",
		success:function(data) {
			var registerState = data.OEStoreRegisterState;
			switch (registerState) {
				case "succeed":
				    localStorage.setItem("OESloginStat", "succeed");
					localStorage.setItem("OESloginName", uname);
					localStorage.setItem("OESloginPass", upass);
					localStorage.setItem("OESLoaded", "false");

				    if(successCall != null) successCall(); 
				    break;
				default:if(failCall != null) failCall(registerState); break;
			}
		}
	});
}